// pages/admin2/admin2.js
const DB = wx.cloud.database().collection("new_menber")
Page({
  data: {
    qq: '',
    img_list: [],
    flag: false,
    level: null,
    grade: null,
    gem: null,
    stone: null,
    name: "",
    openid: '',
    new_qq: [],
    new_qq2: [],
    new_qq3: [],
    flag2: false,
    flag3: false,
    search_qq: ''
  },
  onLoad() {},
  get_qq(event) {
    this.setData({
      qq: event.detail.value
    })
    // console.log(this.data.qq)
  },
  //目前在库未处理的QQ号
  get_qq2() {
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    let that = this;
    let tmp_qqList = [];
    that.setData({
      flag2: false
    })
    DB.where({
      new_success: 'n'
    }).get({
      success: function (res) {
        // console.log("查询在库未处理QQ成功",res.data[0].new_qq)
        // console.log(res.data)
        for (var i = 0; i < res.data.length; i++) {
          let tmp_qqDict = {};
          tmp_qqDict['qq_num'] = res.data[i].new_qq;
          tmp_qqList.push(tmp_qqDict)
        };
        setTimeout(() => {
          that.setData({
            new_qq2: tmp_qqList,
            flag2: true
          });
          // console.log(that.data.new_qq2);
          wx.hideLoading()
        }, 1000);
      }
    })
  },
  //目前在库已处理的QQ号
  get_qq3() {
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    let that = this;
    let tmp_qqList2 = [];
    that.setData({
      flag3: false
    })
    DB.where({
      new_success: 'y'
    }).get({
      success: function (res) {
        // console.log("查询在库已处理QQ成功",res.data[0].new_qq)
        // console.log(res.data.length)
        for (var i = 0; i < res.data.length; i++) {
          let tmp_qqDict = {};
          tmp_qqDict['qq_num'] = res.data[i].new_qq;
          tmp_qqList2.push(tmp_qqDict)
        };
        setTimeout(() => {
          that.setData({
            new_qq3: tmp_qqList2,
            flag3: true
          });
          // console.log(that.data.new_qq3);
          wx.hideLoading()
        }, 1000);
      }
    })
  },
  //获得搜索的数据
  getData() {
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    let that = this;
    // console.log(that.data.qq)
    DB.doc(that.data.qq).get({
      success: function (res) {
        // res.data 包含该记录的数据
        // console.log(res.data)
        setTimeout(() => {
          that.setData({
            img_list: res.data.new_imgList,
            level: res.data.new_level,
            grade: res.data.new_grade,
            gem: res.data.new_gem,
            stone: res.data.new_stone,
            name: res.data.new_name,
            openid: res.data._openid,
            search_qq: res.data.new_qq,
            flag: false
          });
          // console.log("false");
        }, 1000);
        setTimeout(() => {
          that.setData({
            flag: true
          });
          // console.log("true");
          wx.hideLoading()
        }, 1500);
      }
    })
  },
  //下拉刷新
  onPullDownRefresh: function () {
    // console.log("触发刷新");
    var that = this;
    setTimeout(() => {
      that.setData({
        qq: '',
        img_list: [],
        flag: false,
        level: null,
        grade: null,
        gem: null,
        stone: null,
        name: "",
        openid: '',
        new_qq: [],
        new_qq2: [],
        new_qq3: [],
        flag2: false,
        flag3: false,
        search_qq: ''
      })
      that.onLoad(); //重新加载onLoad()
      wx.showToast({
        title: '刷新成功!',
        icon: 'none',
        image: '',
        duration: 1500,
        mask: true,
      })
    }, 1000);
  },
  preview() {
    let that = this;
    let tmp_list = [];
    let num = 0;
    for (var i in that.data.img_list) {
      let tmp_dict = {};
      tmp_dict = that.data.img_list[i];
      tmp_list.push(tmp_dict['imgUrl'])
      num = num + 1;
      // console.log('num:',num)
      // console.log(tmp_list)
    }
    setTimeout(() => {
      if (num == that.data.img_list.length) {
        wx.previewImage({
          current: '', // 当前显示图片的http链接
          urls: tmp_list // 需要预览的图片http链接列表
        })
      }
    }, 1000);
  },

  //审核通过
  successful() {
    wx.showLoading({
      title: '发送中',
      mask: true
    })
    let that = this;
    wx.cloud.callFunction({
      name: 'Subscribe',
      data: {
        name: that.data.name,
        openid: that.data.openid
      },
      success(res) {
        // console.log("发送成功", res)
      },
      fail(res) {
        // console.log("发送失败", res)
      },
    });
    //已阅
    setTimeout(() => {
      // console.log(that.data.search_qq);
      wx.cloud.callFunction({
        name: 'succeed',
        data: {
          qq: that.data.search_qq
        },
        success(res) {
          console.log("发送成功", res)
        },
        fail(res) {
          console.log("发送失败", res)
        },
      });
      wx.hideLoading();
      wx.showToast({
        title: '成功',
        icon: 'success',
        duration: 1500,
        mask: true
      })
    }, 1000);
  },
  //审核淘汰
  failure() {
    wx.showLoading({
      title: '发送中',
      mask: true
    })
    let that = this;
    wx.cloud.callFunction({
      name: 'Subscribe2',
      data: {
        name: that.data.name,
        openid: that.data.openid
      },
      success(res) {
        // console.log("发送成功", res)
      },
      fail(res) {
        // console.log("发送失败", res)
      },
    });
    //已阅
    setTimeout(() => {
      // console.log(that.data.search_qq);
      wx.cloud.callFunction({
        name: 'succeed',
        data: {
          qq: that.data.search_qq
        },
        success(res) {
          console.log("发送成功", res)
        },
        fail(res) {
          console.log("发送失败", res)
        },
      });
      wx.hideLoading();
      wx.showToast({
        title: '成功',
        icon: 'success',
        duration: 1500,
        mask: true
      })
    }, 1000);
  },
  //删除数据
  delete() {
    wx.showModal({
      title: '警告',
      content: '确定要把已处理的数据全部删除吗?',
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定');
          let deleteImg_list = [];
          wx.showLoading({
              title: '删除中',
              mask: true
            }),
            wx.cloud.callFunction({
              name: 'getSuccess_img',
              success(res) {
                // console.log(res.result.data[0].new_imgList)
                for (var i in res.result.data) {
                  // console.log("第一重")
                  for (var j in res.result.data[i].new_imgList) {
                    // console.log("第二重")
                    for (var k in res.result.data[i].new_imgList[j]) {
                      // deleteImg_list.push(res.result.data[i].new_imgList[j])
                      // console.log(res.result.data[i].new_imgList[j]['imgUrl'])
                      deleteImg_list.push(res.result.data[i].new_imgList[j]['imgUrl'])
                    }
                  }
                };
                console.log("获取图片链接完成", res)
              }
            }),
            // console.log("图片链接列表:", deleteImg_list);
          setTimeout(() => {
            wx.cloud.callFunction({
              name: 'deleted_img',
              data: {
                list: deleteImg_list
              },
              success(res) {
                console.log("删除图片完成", res)
              }
            });
            wx.cloud.callFunction({
              name: 'deleted_data',
              success(res) {
                console.log("删除数据完成", res)
              }
            })
          }, 2000);
          setTimeout(() => {
            wx.hideLoading(),
              wx.showToast({
                title: '删除成功',
                icon: 'success',
                duration: 1000,
                mask: true
              })
          }, 3000);
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  }
});